# coding: utf-8
import random

import web

from libs.captcha.captcha import Captcha


class captcha:
    def GET(self):
        cp = Captcha()
        cp.set_type('en')
        cp.set_font_file('fonts/PepRally.ttf')
        cp.set_font_size(60)
        cp.set_font_color(0, 0, 255)
        cp.set_font_rand_a(random.randint(10, 14))
        cp.set_font_rand_b(random.randint(2, 3))
        cp.set_font_rotate(10, 20)
        cp.set_font_distance(35)

        cp.set_board_width(180)
        cp.set_board_height(75)
        cp.set_board_bgcolor(255, 255, 255)

        cp.set_line_num(5)
        cp.set_line_width(2)
        cp.set_line_color(0, 0, 255)
        cp.set_line_rand_a(random.randint(15, 20))
        cp.set_line_rand_b(random.randint(5, 8))

        img_bytes = cp.get_bytes()
        captcha_text = cp.get_text()

        web.ctx.session['captcha'] = captcha_text

        web.header('Content-Type', 'image/png')

        return img_bytes
